package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.client.cmn.DictFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医院的相关操作
 * 上传 查询 基于mongodb
 * @author ybl
 * @version 1.9
 * @date 2021/5/17 9:15
 */
@Service
public class HospitalServiceImpl implements HospitalService {

  @Autowired
  private DictFeignClient dictFeignClient;

  @Autowired
  private HospitalRepository hospitalRepository;

  /**
   * 上传医院接口
   * 上传到mongodb上
   * @param paramMap
   */
  @Override
  public void save(Map<String, Object> paramMap) {
    // 类型转换为model类型
    Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
    // 查看mongodb中是否存在此记录
    Hospital mongoExists = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());

    if (null != mongoExists){ // 不为空则修改
      hospital.setStatus(mongoExists.getStatus());
      hospital.setCreateTime(mongoExists.getCreateTime());
      hospital.setUpdateTime(new Date());
      hospital.setIsDeleted(0);
      hospitalRepository.save(hospital); // 修改
    }else { // 为空则添加
      //0：未上线 1：已上线
      hospital.setStatus(0);
      hospital.setCreateTime(new Date());
      hospital.setUpdateTime(new Date());
      hospital.setIsDeleted(0);
      hospitalRepository.save(hospital); // 添加
    }
  }

  // 查询医院
  @Override
  public Hospital getByHoscode(String hoscode) {
    return hospitalRepository.getHospitalByHoscode(hoscode);
  }

  // 获取分页列表
  @Override
  public Page<Hospital> findHospitalPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {

    // 按照时间降序排序
    Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
    // 0为第一页
    Pageable pageable = PageRequest.of(pageNum-1,pageSize);
    // pageable.getSortOr(sort); // 排序 失败
    Hospital hospital = new Hospital();
    BeanUtils.copyProperties(hospitalQueryVo,hospital); // bean复制
    // 创建匹配器
    ExampleMatcher exampleMatcher = ExampleMatcher.matching() //构建对象
              .withStringMatcher(ExampleMatcher.StringMatcher.STARTING) //改变默认字符串匹配方式:模糊查询
              .withIgnoreCase(true); // 改变默认大小写忽略方式:忽略大小写

    Example<Hospital> example = Example.of(hospital, exampleMatcher);
    Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

    // 封装医院的等级信息
    pages.getContent().stream().forEach(item -> {
      this.packHospital(item);
    });

    return pages;
  }

  // 更新医院上下线
  @Override
  public void lock(String id, Integer status) {
    if (status.intValue() == 0 || status.intValue() == 1){
      Hospital hospital = hospitalRepository.findById(id).get();
      hospital.setUpdateTime(new Date());
      hospital.setStatus(status);
      // 修改
      hospitalRepository.save(hospital);
    }
  }

  // 根据id获取医院详情
  @Override
  public Map<String, Object> showHospDetail(String id) {
    Map<String, Object> hospDetail = new HashMap<>();

    Hospital hospital = hospitalRepository.findById(id).get();
    // 获取医院详情
    packHospital(hospital);
    hospDetail.put("bookingRule",hospital.getBookingRule());
    hospDetail.put("hospital",hospital);
    // 不需要重复返回
    hospital.setBookingRule(null);
    return hospDetail;
  }

  //获取医院名称
  @Override
  public String findHosnameByHoscode(String hoscode) {
    Hospital byHoscode = this.getByHoscode(hoscode);
    if (byHoscode != null){
      return byHoscode.getHosname();
    }
    return null;
  }

  // 根据医院名称关键字搜索医院列表
  @Override
  public List<Hospital> findByHosName(String hosName) {
    List<Hospital> list = hospitalRepository.findHospitalByHosnameLike(hosName);
    return list;
  }

  // 医院预约挂号详情
  @Override
  public Map<String, Object> item(String hoscode) {
    Map<String ,Object> result = new HashMap<>();
    Hospital byHoscode = this.getByHoscode(hoscode);
    packHospital(byHoscode);
    result.put("hospital",byHoscode);
    result.put("bookingRule",byHoscode.getBookingRule());
    // 不再重复获取
    byHoscode.setBookingRule(null);
    return result;
  }

  /**
   * 封装信息
   * 医院等级
   * 医院详细地址
   * @param hospital
   */
  private void packHospital(Hospital hospital) {
    String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
    String provinceName = dictFeignClient.getName(hospital.getProvinceCode()); // 省
    String cityName = dictFeignClient.getName(hospital.getCityCode()); // 城市
    String districtName = dictFeignClient.getName(hospital.getDistrictCode()); // 区

    // 在hospital中添加参数
    hospital.getParam().put("hostypeString",hostypeString); // 医院等级
    hospital.getParam().put("fullAddress",provinceName + cityName  + districtName); // 医院详细地址
  }
}
